package mafiagame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mafiagame.Game.Stage;

import aimclient.ChatConn;
import aimclient.JoscarTester;

public class MafiaClass implements MafiaInterface
{
	JoscarTester aimclient;
	ChatConn chat;

	protected Game g;

	public MafiaClass(JoscarTester communication)
	{
		aimclient = communication;
		g = null;
	}

	public void setChatRoom(ChatConn c)
	{
		chat = c;
		startGame();
	}

	public synchronized void rateLimit()
	{
		try
		{
			Thread.sleep((int) (Math.random() * 1000 + 500));
		} catch (InterruptedException e)
		{
		}
	}

	public void sendPublicMessage(String message)
	{
		rateLimit();
		System.out.println(">" + message);
		chat.sendMsg(message);
	}

	public void sendPrivateMessage(String user, String message)
	{
		rateLimit();
		System.out.println(">" + user + ": " + message);
		aimclient.sendIM(user, message);
	}

	public void startGame()
	{
		if (g == null)
		{
			g = new Game(this);
			g.startGame();
			g.clearActions();
		}
	}

	public synchronized void processHeartbeat()
	{
		g.heartbeat();
	}


	public synchronized void processPublicMessage(String user, String message)
	{
		message = message.toLowerCase();

		if (message.equalsIgnoreCase("in"))
		{
			g.playerIn(user);
		}
		if (message.equalsIgnoreCase("start"))
		{
			g.playersGathered();
		}
		if (message.equalsIgnoreCase("votes"))
		{
			g.printVotes();
		}

		g.executePossibleActionsForUser(user, message, true);

	}

	public synchronized void processPrivateMessage(String user, String message)
	{
		message = message.toLowerCase();
		if (message.equalsIgnoreCase("list"))
		{
			String list = g.getPublicPlayerList();
			sendPrivateMessage(user, list);
		}
		if (message.equalsIgnoreCase("spoilers"))
		{
			Player p = g.getPlayer(user);
			String list = g.getSpoilerPlayerList();
			if (!p.living)
				sendPrivateMessage(user, list);
		}
		if (message.equalsIgnoreCase("help"))
		{
			sendPrivateMessage(user,
					"This is basically an automated version of Chaos Mafia!");
			sendPrivateMessage(user,
					"The currently implemented public commands are: <b>in</b>");
			sendPrivateMessage(
					user,
					"The currently implemented private commands are: <b>list</b>, <b>help</b>, <b>history</b>, <b>actions</b>");
		}
		if (message.equalsIgnoreCase("history"))
		{
			sendPrivateMessage(user, g.getHistory());
		}
		if (message.equalsIgnoreCase("actions"))
		{
			boolean hasRequiredActions = false;
			boolean hasOptionalActions = false;
			String required = "";
			String optional = "";
			if (g.requiredActionsForUser.get(user) != null)
			{
				for (Action a : g.requiredActionsForUser.get(user))
				{
					hasRequiredActions = true;
					if (a.isPublic())
						required = required + "<b>" + a.getCommand() + "</b>: "
								+ a.getDescription() + " (use publically)\n";
					else
						required = required + "<b>" + a.getCommand() + "</b>: "
								+ a.getDescription() + " (use privately)\n";
				}
				if (hasRequiredActions)
					sendPrivateMessage(user,
							"You can use the following actions: \n" + required);
			}
			if (g.optionalActionsForUser.get(user) != null)
			{
				for (Action a : g.optionalActionsForUser.get(user))
				{
					hasOptionalActions = true;
					if (a.isPublic())
						optional = optional + "<b>" + a.getCommand() + "</b>: "
								+ a.getDescription() + " (use publically)\n";
					else
						optional = optional + "<b>" + a.getCommand() + "</b>: "
								+ a.getDescription() + " (use privately)\n";
				}
				if (hasOptionalActions)
					sendPrivateMessage(user,
							"You can use the following actions: \n" + optional);
			}

			if (!hasRequiredActions && !hasOptionalActions)
			{
				sendPrivateMessage(user,
						"It seems that you don't currently have any actions to take.");
				if (g.currentStage == Stage.NIGHT)
					sendPrivateMessage(user,
							"Please hold tight until night is over, then you may talk!");
				if (g.currentStage == Stage.DAY)
					sendPrivateMessage(
							user,
							"Either you don't can't vote for execution this day, you are dead, or this is a bug.");
			}
		}

		g.executePossibleActionsForUser(user, message, false);

	}

	public void removePlayer(String string)
	{
		g.playerOut(string);
	}

}
